package com.zyh.homework.test4;

import java.io.*;
import java.util.Arrays;

/**
 * @Author zyh
 * @Date 2024/8/6 18:48
 * @Description 2.    实现字节数组和任何基本类型和引用类型执行的相互转换
 * 提示：使用ByteArrayInputStream和ByteArrayOutputStream。
 */

public class Test4 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        /*Student student=new Student("zyh",20);
        int num=10;
        boolean flag=true;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeInt(num);
        oos.writeBoolean(flag);
        oos.writeObject(student);
        byte[] bytes = bos.toByteArray();
        oos.flush();
        oos.close();
        bos.close();

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        int i = ois.readInt();
        System.out.println("i = " + i);
        boolean b = ois.readBoolean();
        System.out.println("b = " + b);
        Student stu = (Student) ois.readObject();
        System.out.println("stu = " + stu);*/
        char c='a';
        byte[] method = method(c);
        System.out.println(Arrays.toString(method));

    }

    public static <T> byte[] method(T t) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(t);
        byte[] bytes = bos.toByteArray();
        oos.flush();
        oos.close();
        return bytes;
    }
}
